﻿package com.jxl.dnd.combat
{
	import com.jxl.dnd.abilities.AbilityScores;
	import com.jxl.dnd.events.DnDEvent;
	
	import flash.events.EventDispatcher;
	
	[Event(name="baseAttackBonusChanged", type="com.jxl.dnd.events.DnDEvent")]
	[Event(name="strengthModifierChanged", type="com.jxl.dnd.events.DnDEvent")]
	[Event(name="dexterityModifierChanged", type="com.jxl.dnd.events.DnDEvent")]
	public class AttackBonus extends EventDispatcher
	{
		[Bindable("baseAttackBonusChanged")]
		public var baseAttackBonus:int = 0;
		
		[Bindable("strengthModifierChanged")]
		public function get strengthModifier():Number { return __strengthModifier; }
		
		[Bindable("dexterityModifierChanged")]
		public function get dexterityModifier():Number { return __dexterityModifier; }
		
		protected var __strengthModifier:int = 0;
		protected var __dexterityModifier:int = 0;
		
		protected var __abilityScores:AbilityScores;
		
		public function set abilityScores(val:AbilityScores):void
		{
			if(__abilityScores != null)
			{
				__abilityScores.removeEventListener(DnDEvent.ABILITY_SCORE_CHANGED, onAbilityScoreChanged);
			}
			
			if(val != null)
			{
				__abilityScores = val;
				__abilityScores.addEventListener(DnDEvent.ABILITY_SCORE_CHANGED, onAbilityScoreChanged);
			}
			
			__strengthModifier = __abilityScores.getAbilityModifier(AbilityScores.STRENGTH);
			dispatchEvent(new DnDEvent(DnDEvent.STRENGTH_MODIFIER_CHANGED));
			__dexterityModifier = __abilityScores.getAbilityModifier(AbilityScores.DEXTERITY);
			dispatchEvent(new DnDEvent(DnDEvent.DEXTERITY_MODIFIER_CHANGED));
		}
		
		public function get meleeAttackBonus():Number
		{
			return (baseAttackBonus + strengthModifier);
		}
		
		public function get rangeAttackBonus():Number
		{
			return (baseAttackBonus + dexterityModifier);
		}
		
		public function AttackBonus():void
		{
		}
		
		protected function onAbilityScoreChanged(event:DnDEvent):void
		{
			if(event.abilityScoreType == AbilityScores.STRENGTH)
			{
				__strengthModifier = __abilityScores.getAbilityModifier(AbilityScores.STRENGTH);
				dispatchEvent(new DnDEvent(DnDEvent.STRENGTH_MODIFIER_CHANGED));
			}
			else
			{
				__dexterityModifier = __abilityScores.getAbilityModifier(AbilityScores.DEXTERITY);
				dispatchEvent(new DnDEvent(DnDEvent.DEXTERITY_MODIFIER_CHANGED));
			}
		}
		
		public function toXML():XML
		{
			var abXML:XML = 
				<attackbonus>
					<baseattackbonus>{baseAttackBonus}</baseattackbonus>
					<strengthmodifier>{strengthModifier}</strengthmodifier>
					<dexteritymodifier>{dexterityModifier}</dexteritymodifier>
				</attackbonus>;
			return abXML;
		}
		
		public function buildFromXML(xml:XML):void
		{
			baseAttackBonus			= parseInt(xml.attackbonus.baseattackbonus);
			__strengthModifier		= parseInt(xml.attackbonus.strengthModifier);
			__dexterityModifier		= parseInt(xml.attackbonus.dexteritymodifier);
			dispatchEvent(new DnDEvent(DnDEvent.STRENGTH_MODIFIER_CHANGED));
			dispatchEvent(new DnDEvent(DnDEvent.DEXTERITY_MODIFIER_CHANGED));
		}
	}
}